<?php
/**
* 求职信息采集
* 2012-02-06
* Jken
CREATE TABLE `caiji_rencai_list` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `title` varchar(100) NOT NULL DEFAULT '',
  `pageurl` varchar(200) NOT NULL DEFAULT '',
  `executedid` int(11) NOT NULL DEFAULT 0,
  `scriptname` varchar(50) NOT NULL DEFAULT '',
  PRIMARY KEY (`id`),
  UNIQUE KEY `u_pageurl` (`pageurl`)
)DEFAULT CHARSET=utf8;
alter table qs_jobs_contact add contactimg varchar(100) not null default '';
*/
error_reporting(0);//E_ALL
define("APP_ROOT",dirname(dirname(__FILE__)));
defined("_D_") || define("_D_",DIRECTORY_SEPARATOR);
defined('SCRIPT_NAME') || define("SCRIPT_NAME",basename(__FILE__));
defined('IN_QISHI') || define("IN_QISHI",true);

$jobsChinaLao=new jobsChinaLao();
header("Content-Type:text/html;charset=utf-8");
$jobsChinaLao->pickListInfo();
$jobsChinaLao->pickDetailInfo();

class jobsChinaLao{
	private $caijiCore;
	private $conn;

	public function __construct(){
		$this->caijiCore=false;
		$this->conn=false;

		$this->initCaijiObj();
		$this->initDbObj();
	}

	protected function initDbObj(){
		if($this->conn){
			return($this->conn);
		}

		require_once(APP_ROOT._D_."data"._D_."config.php");
		require_once(APP_ROOT._D_.'include'._D_.'common.inc.php');
		require_once(APP_ROOT._D_."include"._D_."mysql.class.php");
		$this->conn = new mysql($dbhost,$dbuser,$dbpass,$dbname,'utf8');
		$this->conn->showError=false;

		return($this->conn);
	}

	protected function initCaijiObj(){
		if($this->caijiCore){
			return ($this->caijiCore);
		}
		require_once(APP_ROOT._D_."jkenCaiji"._D_."caiji.inc.php");
		$this->caijiCore = new caijiCore();
		$this->caijiCore->cTimeOut=8;
		$this->caijiCore->textTimeOut=20;
		$this->caijiCore->pageCharset = "utf-8";

		return ($this->caijiCore);
	}

	public function memberLogin(){
		$submitUrl="http://id.chinalao.com/default.aspx";

		$memberInfo=array();
		$memberInfo["txtUserName"]="chinalao88";
		$memberInfo["txtPwd"]="chinalao88";
		$memberInfo["__VIEWSTATE"]="/wEPDwUKMjEwMDQ0NjM4NQ9kFgJmD2QWBAIHDxYCHgdWaXNpYmxlaGQCCQ8WAh4FdmFsdWUFG2h0dHA6Ly9yZW5jYWkuY2hpbmFsYW8uY29tL2Rk6Q4M1v5HT7nA3yblKw/CBZ0dSVA=";
		$memberInfo["btnLogin"]="登录";
		$loginedRegExp='#会员登录#';

		$logined = $this->caijiCore->toLogin($submitUrl,$memberInfo,$loginedRegExp);
		
		return ($logined);
	}

	public function pickListInfo(){
		$listPageTpl="http://zhaogong.chinalao.com/c_j_dt0_t_k_f0_r0_z0_s0_y0_o0_p";
		$firstListPage=$listPageTpl.'1';
		$this->caijiCore->url=$firstListPage;
		$this->caijiCore->pickData();
		$totalPages=$this->caijiCore->getTotalPages('#<a class="page" href="/c_j_dt0_t_k_f0_r0_z0_s0_y0_o0_p(\d+)">尾页</a>#i');
		$totalPages=intval($totalPages);
		$endDataValue=strtotime("-2 days");

		for($curPage=1;$curPage<=$totalPages;$curPage++){
			$this->caijiCore->url=$listPageTpl.$curPage;
			$this->caijiCore->pickData();
			$this->caijiCore->data=$this->caijiCore->cutData('<div class="list-content">','<!--list-content-->','in');
			preg_match_all('/更新时间：([\d\-]+)/' , $this->caijiCore->data , $timeMatch);
			$tmpData=$this->caijiCore->parseDataAll('#<h3><a target="_blank" href="([^"]+)" title="招工信息：([^"]+)">#is');
			foreach($tmpData[1] as $index => $pageUrl){
				$title=$tmpData[2][$index];
				$tmptime=strtotime($timeMatch[1][$index]);

				$dataRow=array();
				$dataRow["title"]=$title;
				$dataRow["pageurl"]=$pageUrl;
				$dataRow["executedid"]='0';
				$dataRow["scriptname"]=SCRIPT_NAME;
				$listId=$this->conn->insert($dataRow,'caiji_rencai_list',' insert ');
				$error=' listId:'.$listId;
				if(!$listId && mysql_errno()==1062){
					$error='Duplicate entry';
				}
				$this->printInfo($curPage,$totalPages,$error);
				if($tmptime < $endDataValue){
					return true;
				}
			}
		}
	}

	public function pickDetailInfo(){
		$sql="select id,title,pageurl from caiji_rencai_list where executedid='' and scriptname='".SCRIPT_NAME."' order by id";
		$listData=$this->conn->getall($sql);
		foreach($listData as $tmpData){
			$caijiId=$tmpData["id"];
			$title=$tmpData["title"];
			$pageurl=$tmpData["pageurl"];
			$pageurl=rtrim($pageurl,'/');
			$pageurl.='/index.html';
			$this->caijiCore->url=$pageurl;
			$this->caijiCore->pickData();
			$this->caijiCore->data=$this->caijiCore->cutData('<div class="grid-content">','<strong>留言</strong>','in');

			$dataRow=array();
			$dataRow["uid"]=1;
			$dataRow["subsite_id"]=0;
			$dataRow["tpl"]='';

			//职位名称
			$jobs_name=addslashes($title);
			$dataRow["jobs_name"]=$jobs_name;

			//性别要求
			$dataRow["sex"]='3';
			$dataRow["sex_cn"]='不限';
			$matches = $this->caijiCore->parseDataOnce('#<li><span class="item-label">男女比例：</span>([^<]+)</li>#is');
			if($matches){
				$sex = trim($matches[1]);
				if($sex=='限男'){
					$dataRow["sex"]='1';
					$dataRow["sex_cn"]='男';
				}elseif($sex=='限女'){
					$dataRow["sex"]='2';
					$dataRow["sex_cn"]='女';
				}
			}

			//职位性质
			$dataRow["nature"]='64';
			$dataRow["nature_cn"]='实习';
			$matches = $this->caijiCore->parseDataOnce('#<li><span class="item-label">工作性质：</span>([^<]+)</li>#is');
			if($matches){
				$nature = trim($matches[1]);
				if($nature=='全职'){
					$dataRow["nature"]='62';
					$dataRow["nature_cn"]='全职';
				}elseif($nature=='兼职'){
					$dataRow["nature"]='63';
					$dataRow["nature_cn"]='兼职';
				}
			}


			//招聘人数
			$dataRow["amount"]='0';
			$matches = $this->caijiCore->parseDataOnce('#<li><span class="item-label">招聘人数：</span>(\d+)(人)?</li>#is');
			if($matches){
				$amount = intval($matches[1]);
				$dataRow["amount"]=$amount;
			}
			
			//添加时间
			$dataRow["addtime"]=time();
			$matches = $this->caijiCore->parseDataOnce('#更新时间：<em class="info-update-date">([^<]+)<#is');
			if($matches){
				$addtime = trim($matches[1]);
				$addtime = strtotime($addtime);
				$dataRow["addtime"]=$addtime;
			}
			
			//终止时间
			$dataRow["deadline"]='0';
			$addtime = date('Y-m-d',$dataRow["addtime"]);
			$deadline = strtotime("$addtime +30days");
			$dataRow["deadline"]=$deadline;

			//刷新时间
			$dataRow["refreshtime"]=$dataRow["addtime"];

			//职位类别
			$dataRow["category"]='0';
			$dataRow["subclass"]='0';
			$dataRow["category_cn"]='';

			//职位类别
			$dataRow["trade"]='0';
			$dataRow["trade_cn"]='';

			//工作地区
			$dataRow["district"]=0;
			$dataRow["district_cn"]='';
			$cateidArr=array();
			$districtStr='';
			$matches = $this->caijiCore->parseDataOnce('#工作地区：</span>([^<]+)</li>#is');
			if($matches){
				$district_cn = trim($matches[1]);
				//江苏-南通-启东
				$districtArr=explode(',',$district_cn);

				$cateRow = $this->districtInfo($districtArr[0]);
				if($cateRow){
					$cateidArr[0]=$cateRow["id"];
					$districtStr.="/" . $cateRow["categoryname"];
				}

				$cateRow = $this->districtInfo($districtArr[1]);
				if($cateRow){
					$cateidArr[1]=$cateRow["id"];
					$districtStr.="/" . $cateRow["categoryname"];
				}
			}
			$dataRow["district"]=isset($cateidArr[0]) ? $cateidArr[0] : 0;
			$dataRow["sdistrict"]=isset($cateidArr[1]) ? $cateidArr[1] : 0;
			$dataRow["district_cn"]=trim($districtStr,'/');

			//学历要求
			$education=array(65=>'初中',66=>'高中',67=>'中技',68=>'中专',69=>'大专',86=>'其它');
			$key = array_rand($education,1);
			$dataRow["education"] = $key;
			$dataRow["education_cn"] = $education[$key];

			//工作经验
			$experience=array(74=>'无经验',75=>'1年以下',76=>'1-3年');
			$key = array_rand($experience,1);
			$dataRow["experience"] = $key;
			$dataRow["experience_cn"] = $experience[$key];
			
			//月薪范围
			$wageArr=array(55=>'',56=>'1000~1500',57=>'1500~2000',58=>'2000~3000',59=>'3000~5000',60=>'5000~10000',61=>'1万以上/月');
			$dataRow["wage"]=55;
			$dataRow["wage_cn"]='面议';
			$matches = $this->caijiCore->parseDataOnce('#<li class="info-wage-zoom"><span class="item-label">收入范围：</span><em>([^<]+)</em>#is');
			if($matches){
				$wage = trim($matches[1]);
				$wage = intval($wage);
				if($wage<=1000){
					$dataRow["wage"]=55;
					$dataRow["wage_cn"]='面议';
				}elseif($wage<=1500){
					$dataRow["wage"]=56;
					$dataRow["wage_cn"]='1000~1500元/月';
				}elseif($wage<=2000){
					$dataRow["wage"]=57;
					$dataRow["wage_cn"]='1500~2000元/月';
				}elseif($wage<=3000){
					$dataRow["wage"]=58;
					$dataRow["wage_cn"]='2000~3000元/月';
				}elseif($wage<=5000){
					$dataRow["wage"]=59;
					$dataRow["wage_cn"]='3000~5000元/月';
				}elseif($wage<=10000){
					$dataRow["wage"]=60;
					$dataRow["wage_cn"]='5000~10000元/月';
				}else{
					$dataRow["wage"]=61;
					$dataRow["wage_cn"]='1万以上/月';
				}
			}
			
			//职位描述
			$dataRow["contents"]='';
			$matches = $this->caijiCore->parseDataOnce('#<strong>职位说明：</strong></dt>\s+<dd class="item-dec">\s+(.+?)\s+</dd>#is');
			if($matches){
				$contents = trim($matches[1]);
				$contents = strip_tags($contents);
				$contents = str_replace('中劳网','久久招工网',$contents);
				$dataRow["contents"]=addslashes($contents);
			}
			
			//每周休息
			$dataRow["rest"]='1';
			$matches = $this->caijiCore->parseDataOnce('#<li><span class="item-label">月工作天数：</span>([^>]+)</li>#is');
			if($matches){
				$rest = intval($matches[1]);
				if($rest > 24){
					$rest = 1;
				}elseif($rest >20){
					$rest = 2;
				}else{
					$rest = 0;
				}
				$dataRow["rest"]=$rest;
			}
			
			$dataRow["companyname"]='';
			$dataRow["company_id"]=0;
			$dataRow["company_addtime"]=0;
			$dataRow["graduate"]='1';
			$dataRow["highlight"]='';
			
			//缴纳保险保险
			$dataRow["insurance"]='0';
			$dataRow["audit"]='1';
			$dataRow["display"]='1';
			$dataRow["user_status"]='1';

			$key=array();
			$len = mb_strlen($dataRow["district_cn"],"utf-8");
			for($s=0;$s<$len;$s++){
				$char=mb_substr($dataRow["district_cn"],$s,1,'utf-8');
				$key[$s]=$char;
			}
			$len = mb_strlen($dataRow["jobs_name"],"utf-8");
			for($ss=0;$ss<$len;$ss++){
				$char=mb_substr($dataRow["jobs_name"],$ss,1,'utf-8');
				$key[$s+$ss]=$char;
			}
			$dataRow["key"]=implode('000 ',$key);
			$dataRow["robot"]='1';

			/*
				联系方式的读取
			*/
			//联 系 人
			$contactRow=array();
			$contactRow["contact"]='';
			$matches = $this->caijiCore->parseDataOnce('#<dt class="item-name"><strong>([^<]+)</strong>#is');
			if($matches){
				$contact = trim($matches[1]);
				$contactRow["contact"]=addslashes($contact);
			}
			$contactRow["telephone"]='';
			$contactRow["address"]='';
			$contactRow["email"]='';
			$contactRow["notify"]='0';
			
			//联系方式图片
			$contactRow["contactimg"]='';
			$contactImg=false;
			$matches = $this->caijiCore->parseDataOnce('#background:url\(([^\(]+)\)#is');
			if($matches){
				$contactImg = trim($matches[1]);
			}

			if($contactImg){
				$dirImg="temp"._D_."contactImg";

				$basename = substr($contactImg , strrpos($contactImg,'/'));
				$basename = trim($basename,'/');
				$this->caijiCore->saveImg($contactImg,$basename,$dirImg);
				$tmpcontactImg=addslashes('/temp/contactImg/'.$basename);
				$contactRow["contactimg"]=$tmpcontactImg;
			}
			/*****************************************************************************/
			$sql="select qs_jobs.id from qs_jobs inner join qs_jobs_contact on qs_jobs_contact.pid=qs_jobs.id where qs_jobs.jobs_name='$jobs_name' and qs_jobs_contact.contactimg='$tmpcontactImg'";
			$rs = $this->conn->getone($sql);
			$jobexists=false;
			if(!empty($rs['id'])){
				$jobexists=true;
				$jobId=intval($rs['id']);
			}else{
				$jobId=$this->conn->insert($dataRow,'qs_jobs',' insert ');
			}
			$contactRow["pid"]=$jobId;

			
			$msg=' jobId:'.$jobId;
			if($jobId > 0){
				$sql="update caiji_rencai_list set executedid=".$jobId." where id =".$caijiId;
				$this->conn->query($sql);

				//alter table qs_jobs_contact add contactimg varchar(100) not null default '';
				if(!$jobexists){
					$contactId=$this->conn->insert($contactRow,'qs_jobs_contact',' insert ');
					$msg.=' contactId:'.$contactId;
				}
			}elseif(mysql_errno()==1062){
				$error='Duplicate entry';
			}

			$this->printInfo($curPage,$totalPages,$msg);
		}//end of foreach
	}

	protected function districtInfo($categoryname,$pid=false){
		$row = false;
		$sql="select id,categoryname from qs_category_district where 1";
		if($pid!==false){
			$sql .=" and parentid=".$pid;
		}
		$sql .=" and categoryname like '".$categoryname."%'";
		$row = $this->conn->getone($sql);
		
		return ($row);
	}

	protected function printInfo($curPage,$totalPages,$error){
		$outputInfo = "current page: ".$curPage."/".$totalPages;
		$outputInfo.=" ".$error;
		$outputInfo.=' --- '.$this->memoryInfo();
		$outputInfo.="<br />\n";

		echo $outputInfo;
	}

	/*
		获取当前内存占用情况
	*/
	public function memoryInfo(){
		$curMemory = memory_get_usage(true);
		$peakMemory = memory_get_peak_usage(true);

		$curMemory = $curMemory/1024/1024;
		$peakMemory = $peakMemory/1024/1024;

		$curMemory = number_format($curMemory,2,'.','');
		$peakMemory = number_format($peakMemory,2,'.','');

		$memoryInfo=$curMemory.'mb/'.$peakMemory.'mb';

		return ($memoryInfo);
	}
}